
import { _decorator, Component, Node, v3, Tween, tween, Vec3 } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import { EventType } from '../../Script/GameSpecial/GameEventType';
const { ccclass, property } = _decorator;

@ccclass('SettingCmp')
export class SettingCmp extends yyComponent {
    @property(Node)
    protected animNode: Node = null;

    @property(Node)
    protected audioBtn: Node = null;

    @property(Node)
    protected shakeBtn: Node = null;

    protected _isShow = false;

    onLoad() {
        this.hideAnim(false);
    }

    onEnable() {
        this.check();
    }

    onDisable() {
        this.hideAnim(false);
    }

    protected check() {
        this.setAudioSp(GameConfig.audioConfig.bgm);
        this.setShakeSp(GameConfig.driveConfig.vibrate);
    }

    setAudioSp(isOn: boolean) {
        let on = this.audioBtn.getChildByName('on');
        let off = this.audioBtn.getChildByName('off');
        on.active = isOn;
        off.active = !isOn;
    }
    setShakeSp(isOn: boolean) {
        let on = this.shakeBtn.getChildByName('on');
        let off = this.shakeBtn.getChildByName('off');
        on.active = isOn;
        off.active = !isOn;
    }

    protected onAudioClick() {
        let config = GameConfig.audioConfig.bgm;
        config = !config;
        let audioConfig = {
            bgm: config,
            effect: config,
        };
        GameConfig.audioConfig = audioConfig;
        if (!!config) {
            this.emit(EventType.Common.AudioEvent.resume);
        } else {
            this.emit(EventType.Common.AudioEvent.pause);
        }
        this.check();

        this.emit(EventType.Common.AudioEvent.playClickBtn);
    }

    protected onShakeClick() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);

        let config = GameConfig.driveConfig.vibrate;
        GameConfig.driveConfig.vibrate = !config;
        this.check();
    }
    //外部按钮调用
    public onShowSetting() {
        this.node.active = true;
        this._isShow = !this._isShow;
        if (this._isShow) {
            this.showAnim();
        } else {
            this.hideAnim();
        }
    }
    //只执行关闭
    public onHideSetting() {
        if (!this.node.active) return;
        this._isShow = false;
        this.hideAnim();
    }

    //动画
    _scale = v3();
    showAnim() {
        Tween.stopAllByTarget(this._scale);
        this._scale.set(0, 0, 0);
        tween(this._scale).to(0.4, { x: 1, y: 1, z: 1 }, {
            easing: 'backOut', onUpdate: () => {
                this.animNode.setScale(this._scale);
            }
        }).start();
    }

    hideAnim(isAnim = true) {
        if (!isAnim) {
            this._scale.set(Vec3.ZERO);
            this.animNode.setScale(this._scale);
            return;
        }
        Tween.stopAllByTarget(this._scale);
        tween(this._scale).to(0.4, { x: 0, y: 0, z: 0 }, {
            easing: 'backIn', onUpdate: () => {
                this.animNode.setScale(this._scale);
            }
        }).start();
    }
}
